package com.hotel.BL;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.HouseKeepingUser;

public class HouseKeepingUserLogic extends AbstractBL{
	
	public HouseKeepingUserLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}

	public List<HouseKeepingUser> GenerateHouseKeepingUser(long hotelId)
	{
		List<HouseKeepingUser> list = new ArrayList<HouseKeepingUser>();
		HouseKeepingUser  housekeepinguser;
		if(GetHouseKeepingUser(hotelId,"Chonlapat")==null){
			housekeepinguser = new HouseKeepingUser();
			housekeepinguser.setHotelId(hotelId);
			housekeepinguser.setFirstName("Chonlapat");
			housekeepinguser.setAddress("address");
			housekeepinguser.setDateOfJoin(new Date());
			housekeepinguser.setState("");
			housekeepinguser.setCity("");
			housekeepinguser.setZipCode("");
			housekeepinguser.setPhone("");
			housekeepinguser.setMobile("");
			housekeepinguser.setEmergencyNumber("");
			housekeepinguser.setGender("M");
			//user.setBirthday(Date);
			housekeepinguser.setIdentificationNo("");
			housekeepinguser.setPassportNumber("");
			housekeepinguser.setBloodGroup("O");
			housekeepinguser.setDesignation("");
			list.add(housekeepinguser);
		}
		if(GetHouseKeepingUser(hotelId,"Test")==null){
			housekeepinguser = new HouseKeepingUser();
			housekeepinguser.setHotelId(hotelId);
			housekeepinguser.setFirstName("Test");
			housekeepinguser.setAddress("address");
			housekeepinguser.setDateOfJoin(new Date());
			housekeepinguser.setState("");
			housekeepinguser.setCity("");
			housekeepinguser.setZipCode("");
			housekeepinguser.setPhone("");
			housekeepinguser.setMobile("");
			housekeepinguser.setEmergencyNumber("");
			housekeepinguser.setGender("M");
			//user.setBirthday(Date);
			housekeepinguser.setIdentificationNo("");
			housekeepinguser.setPassportNumber("");
			housekeepinguser.setBloodGroup("O");
			housekeepinguser.setDesignation("");
			list.add(housekeepinguser);
		}
		return list;
	}
	
	public HouseKeepingUser GetHouseKeepingUser(long hotelId,String title)
	{
		Query q = new Query(EntityName.HouseKeepingUser);
		
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		
		Filter filters = CompositeFilterOperator.and(filterHotel, filterTitle);
		q.setFilter(filters);
		
		return GetHouseKeepingUser(datastore.prepare(q).asSingleEntity());
	}
	
	public List<HouseKeepingUser> GetAllHouseKeepingUser(long hotelId)
	{
		Query q = new Query(EntityName.HouseKeepingUser);
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		q.setFilter(filterHotel);
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public HouseKeepingUser GetHouseKeepingUser(Entity entity)
	{
		if(entity == null) return null;
		HouseKeepingUser item = new HouseKeepingUser();
		item.setEntity(entity);
		return item;
	}
	
	public List<HouseKeepingUser> GetList(List<Entity> entities)
	{
		List<HouseKeepingUser> list = new ArrayList<HouseKeepingUser>();
		if(entities != null)
			for(Entity e : entities)
			{
				HouseKeepingUser t = new HouseKeepingUser();
				t.setEntity(e);
				list.add(t);
			}
		return list;
	}
	
	public List<HouseKeepingUser> GetAllHouseKeepingUser4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.HouseKeepingUser);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
	
	
	public List<HouseKeepingUser> GetAllHouseKeepingUser4Admin(long hotelId)
	{
		Query q = new Query(EntityName.HouseKeepingUser);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public HouseKeepingUser ChangeStatus(long amenId) throws EntityNotFoundException{
		
		HouseKeepingUser a = GetHouseKeepingUser(amenId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public HouseKeepingUser GetHouseKeepingUser(long id) {
		Key key = KeyFactory.createKey(EntityName.HouseKeepingUser, id);
		try {
			Entity lang = datastore.get(key);
			HouseKeepingUser c = new HouseKeepingUser();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public HouseKeepingUser Delete(long id) throws EntityNotFoundException{
		HouseKeepingUser t = GetHouseKeepingUser(id);
		t.setAvaliable(false);
		t.setStatus(false);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);
		return t;
	}
	
	public HouseKeepingUser Add(long hotelId,String address,Date birthday,
			String bloodGroup,String  city,String country,Date dateOfJoin,String designation,
			String driverLicense,String emergencyNumber,String firstName,String gender,
			String identificationNo,String lastName,String mobile,String passportNumber,
			String phone,String socialSecurityCard,String state,String zipCode) throws EntityNotFoundException{
		HouseKeepingUser a = new HouseKeepingUser();
		
		a.setAddress(address);
		a.setBirthday(birthday);
		a.setBloodGroup(bloodGroup);
		a.setCity(city);
		a.setCountry(country);
		a.setDateOfJoin(dateOfJoin);
		a.setDesignation(designation);
		a.setDriverLicense(driverLicense);
		a.setEmergencyNumber(emergencyNumber);
		a.setFirstName(firstName);
		a.setGender(gender);
		a.setHotelId(hotelId);
		a.setIdentificationNo(identificationNo);
		a.setLastName(lastName);
		a.setMobile(mobile);
		a.setPassportNumber(passportNumber);
		a.setPhone(phone);
		a.setSocialSecurityCard(socialSecurityCard);
		a.setState(state);
		a.setZipCode(zipCode);
		a.setAvaliable(true);
		a.setStatus(true);
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public HouseKeepingUser Edit(long id,String address,Date birthday,
			String bloodGroup,String  city,String country,Date dateOfJoin,String designation,
			String driverLicense,String emergencyNumber,String firstName,String gender,
			String identificationNo,String lastName,String mobile,String passportNumber,
			String phone,String socialSecurityCard,String state,String zipCode)
			throws EntityNotFoundException{
		HouseKeepingUser a = GetHouseKeepingUser(id);
		
		a.setAddress(address);
		a.setBirthday(birthday);
		a.setBloodGroup(bloodGroup);
		a.setCity(city);
		a.setCountry(country);
		a.setDateOfJoin(dateOfJoin);
		a.setDesignation(designation);
		a.setDriverLicense(driverLicense);
		a.setEmergencyNumber(emergencyNumber);
		a.setFirstName(firstName);
		a.setGender(gender);
		a.setIdentificationNo(identificationNo);
		a.setLastName(lastName);
		a.setMobile(mobile);
		a.setPassportNumber(passportNumber);
		a.setPhone(phone);
		a.setSocialSecurityCard(socialSecurityCard);
		a.setState(state);
		a.setZipCode(zipCode);
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
}
